package com.cloudlead.common.worker.impl;

import java.util.Comparator;

import com.cloudlead.common.worker.Interceptor;
import com.cloudlead.common.worker.annotation.Order;

public class InterceptorOrderComparator implements Comparator<Interceptor> {

	@Override
	public int compare(Interceptor i1, Interceptor i2) {
		Order o1 = i1.getClass().getAnnotation(Order.class);
		Order o2 = i2.getClass().getAnnotation(Order.class);
		int n1 = o1 == null ? Integer.MAX_VALUE : o1.value();
		int n2 = o2 == null ? Integer.MAX_VALUE : o2.value();
		if (n1 == n2)
			return i1.getClass().getName().compareTo(i2.getClass().getName());
		return n1 < n2 ? (-1) : 1;
	}
}
